package com.atguigu.spring.xml;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

//基于xml
@Component
public class LoggerAspect {

    public void beforeAdviceMethod(JoinPoint joinPoint){
        //获取连接点所对应的方法的签名信息(方法的)
        Signature signature = joinPoint.getSignature();
        //获取连接点所对应的方法中的参数
        Object[] args = joinPoint.getArgs();
        System.out.println("LoggerAspect,前置通知xmlxmlxmlxml");
        //System.out.println("LoggerAspect:方法"+signature.getName()+",参数："+ Arrays.toString(args));
    }

    public void afterAdviceMethod(){
        System.out.println("LoggerAspect,后置通知()xmlxmlxmlxml");
    }


    public void afterReturningAdviceMethod(JoinPoint joinPoint,Object result){
        Signature signature = joinPoint.getSignature();
        System.out.println("LoggerAspect,方法名:"+signature.getName()+",结果"+result);
    }

    public void afterThrowingAdviceMethod(JoinPoint joinPoint,Throwable ex){
        Signature signature = joinPoint.getSignature();
        System.out.println("LoggerAspect，方法"+signature.getName()+",异常："+ex);
    }


    //环绕通知的方法的返回值一定要与目标对象的返回值一致
    public Object aroundAdviceMethod(ProceedingJoinPoint joinPoint){
        Object proceed = null;
        try {
            System.out.println("环绕通知，前置通知-->");
            //可执行连接点的信息；表示目标对象执行的方法
            proceed = joinPoint.proceed();
            System.out.println("环绕通知，返回通知-->");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            System.out.println("环绕通知，异常通知-->");
        }finally {
            System.out.println("环绕通知，后置通知");
        }
        return proceed;
    }
}
